/**
 * @author Andrew Dashin <dashin@andrewdashin.com>
 */
var MyRegistrationBox = Class.create();
MyRegistrationBox.prototype = Object.extend(new Service(), {
    ERR_EMPTY_LOGIN: '1',
    ERR_EMPTY_PASSWORD: '2',
    ERR_PASSWORDS_NOT_MATCH: '3',
    ERR_EMPTY_FIRSTNAME: '4',
    ERR_EMPTY_LASTNAME: '5',
    ERR_INVALID_EMAIL: '6',
    ERR_WEAK_PASSWORD: '7',
    ERR_LOGIN_EXISTS: '8',
    ERR_EMAIL_EXISTS: '9',
    ERR_EMAIL_SENDING: '10',
	initialize: function () {
		// init action
	},
    
    bind: function (oData) {
        this.login = oData.login;
        this.password = oData.password;
        this.confirmPassword = oData.confirmPassword;
        this.firstName = oData.firstName;
        this.lastName = oData.lastName;
        this.email = oData.email;
        this.error = oData.error;
        this.submitButton = oData.submit;
        var obj = this;
        this.submitButton.onclick = function () {
            obj.submit();
        };
    },
	
    onResponse: function (oResponse) {
        switch (oResponse.transport.responseText) {
            case '0':
                // do register
                // Show success message
                this.error.innerHTML = 'You were successfully registred';
                break;
            default:
                this.showError(oResponse.transport.responseText);
                break;
        }
    },
    
    onFailure: function (oResponse) {
        alert('[LogIn] Somethig goes wrong...');
    },
    
	showError: function (sCode) {
        switch (sCode) {
	        case this.ERR_EMPTY_LOGIN:
	           this.error.innerHTML = 'Login is empty';
	           break;
	        case this.ERR_EMPTY_PASSWORD:
	           this.error.innerHTML = 'Password is empty!';
	           break;
	        case this.ERR_PASSWORDS_NOT_MATCH:
	           this.error.innerHTML = 'Passwords do not match';
	           break;
	        case this.ERR_EMPTY_FIRSTNAME:
	           this.error.innerHTML = 'Empty First Name';
	           break;
	        case this.ERR_EMPTY_LASTNAME:
	           this.error.innerHTML = 'Empty last name';
	           break;
	        case this.ERR_INVALID_EMAIL:
	           this.error.innerHTML = 'Invalid email';
	           break;
	        case this.ERR_WEAK_PASSWORD:
	           this.error.innerHTML = 'Password is too weak';
	           break;
	        case this.ERR_LOGIN_EXISTS:
	           this.error.innerHTML = 'User with this login already registred';
	           break;
	        case this.ERR_EMAIL_EXISTS:
	           this.error.innerHTML = 'User with this emil already registred';
	           break;
	        case this.ERR_EMAIL_SENDING:
	           this.error.innerHTML = 'Error sending email, please try later';
	           break;
           default:
               this.error.innerHTML = 'Exception: ' + sCode;
               break;
	    }
	},
	
    /**
    * @method
    * @description
    */ 
	check: function () {
		// this should validate input
        return true;
	},
	
    /**
    * @method
    * @description
    */ 
	submit: function () {
		// submit action and returned result check should be made here
        if (this.check()) {
            // make request
            var obj = this;
            Service.prototype.initialize.call(this, {
                    onResponseRecieved: function (t) {obj.onResponse(t)},
                    onFailure:          function (t) {obj.onFailure(t)},
                    parameters:         {
                                          service:          'registration',
                                          login:            obj.login.value,
                                          password:         obj.password.value,
                                          confirmpassword:  obj.confirmPassword.value,
                                          firstname:        obj.firstName.value,
                                          lastname:         obj.lastName.value,
                                          email:            obj.email.value
                                        }
                    }
            );
        }
	}
});
